<!--vue模板-->
<template>
  <div>
    <!--搜索-->
    <div class="toolbar">
      <el-input
        placeholder="请输入标题"
        clearable
        style="width: 200px; margin-right: 10px"
        v-model="filters.type"
      ></el-input>
      &nbsp;
      <el-button type="primary" @click="init">查询</el-button>
      <el-button type="primary" @click="reset">重置</el-button>
    </div>
    <!--按钮区-->
    <div class="toolbar">
      <el-button
      type="primary"
      @click="save()"
      >
      添加
      </el-button>
    </div>
    <!--表格数据-->
    <el-table :data="tableData" style="width: 100%" stripe border>
      <el-table-column
        label="序号"
        type="index"
        width="102"
        header-align="center"
        align="center"
      />
      <el-table-column
        prop="dictCode"
        label="字典编码"
        width="150"
        header-align="center"
      />
      <el-table-column
        prop="dictName"
        label="字典名"
        width="300"
        header-align="center"
      />
      <el-table-column
        prop="dictValue"
        label="字典值"
        width="300"
        header-align="center"
      />
      <el-table-column
        prop="createTime"
        label="创建时间"
        header-align="center"
      />
      <el-table-column
        label="操作"
        header-align="center"
        align="center"
        width="200"
      >
        <template #default="{ row }">
          <el-button
            type="primary"
            :icon="Edit"
            circle
            title="编辑"
            @click="save(row)"
          />
          <el-button
          type="danger"
          :icon="Delete"
          circle
          title="删除"
          @click= "del(row.id)"
          />
        </template>
      </el-table-column>
    </el-table>
    <!--新增-->
    <SaveOrUpdate
      ref="saveOrUpdate"
      :visible="isShow"
      @close="isShow = false"
      @refresh="init()"
    />
  </div>
</template>

<!--vue 组件选项定义-->
<script setup>
import { ref, onBeforeMount } from "vue";
import { Delete, Edit } from "@element-plus/icons-vue";
import { queryDict } from "@/api/dict.js";
import { ElMessage, ElMessageBox } from "element-plus";
import SaveOrUpdate from "./SaveOrUpdate.vue";
//表格数据
const tableData = ref([]);
//是否显示新增用户
const isShow = ref(false);
const saveOrUpdate = ref(null);
const type = ref(null);

//搜索 条件
const filters = ref({
  type: ""
});

//重置
function reset() {
  filters.value = {
    type: ""
  };
  init();
}

//新增
function save(data) {
  isShow.value = true;
  saveOrUpdate.value.init(data);
}

//删除
function del(id) {
  ElMessageBox.confirm("你确定要删除吗?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    })
    .then(() => {
      todel(id);
    })
    .catch(() => {});
}

async function todel(id) {
  console.log("=============", id);
  let res = await deletemsg(id)
  if(res.code == 200){
    ElMessage({
      message: "删除成功",
      type: "success"
    });
    init();
  }
}

//方法
async function init() {
  let params =  filters.value.type;

  console.log("===============---->", params);
  let res = await queryDict( params );
  if (res.code == 200) {
    tableData.value = res.data;
  }
}

//生命周期钩子
onBeforeMount(() => {
  init();
});
</script>

<!--vue样式 scoped当前样式只适用于当前组件-->
<style scoped>
.toolbar {
  height: 60px;
  line-height: 60px;
  background-color: #fff;
  padding-left: 20px;
}
</style>
